package com.lzh.controller;

import cn.hutool.core.bean.BeanUtil;
import com.lzh.bean.Customer;
import com.lzh.bean.Supplier;
import com.lzh.bean.SupplierContact;
import com.lzh.common.bean.PageBean;
import com.lzh.common.bean.ResultBean;
import com.lzh.common.controller.BaseController;
import com.lzh.common.group.V;
import com.lzh.common.utils.Assert;
import com.lzh.query.CustomerQuery;
import com.lzh.query.SupplierContactQuery;
import com.lzh.query.SupplierQuery;
import com.lzh.service.SupplierContactService;
import com.lzh.service.impl.SupplierServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.DayOfWeek;
import java.util.List;

@RestController
@RequestMapping("/api")
@Api(tags = "供应商管理")
public class SupplierController extends BaseController {
    
    @Autowired
    SupplierServiceImpl supplierService;

    @Autowired
    SupplierContactService supplierContactService;;
    /**
     * 添加供应商
     * @param supplier
     * @return
     */
    @ApiOperation("保存供应商")
    @PostMapping("/supplier")
    public ResultBean saveSupplier(@Validated @RequestBody Supplier supplier, BindingResult bindingResult) {
        V.validate(bindingResult);
        int save = supplierService.save(supplier);
        Assert.isZerp(save,"保存失败");
        return success("保存成功");
    }

    /**
     * 查询供应商
     * @param
     * @return
     */
    @ApiOperation("查询供应商")
    @GetMapping("/supplier")
    public ResultBean getSupplierById(int id) {
        Supplier supplier = supplierService.querySupplierById(id);
        return success("供应商信息",supplier);
    }


    /**
     * 更新供应商
     * @param supplier
     * @return
     */
    @ApiOperation("更新供应商")
    @PutMapping("/supplier")
    public ResultBean updateSupplier(@RequestBody Supplier supplier) {
        int update = supplierService.updateSupplier(supplier);
        String tmp = update > 0 ? "更新成功":"更新失败";
        return success("更新供应商",tmp);
    }


    /**
     * 查询供应商和联系人列表
     * @param supplierContactQuery
     * @return
     */
    @ApiOperation("查询供应商和联系人列表")
    @PutMapping("/supplier/list")
    public ResultBean<PageBean<List<Supplier>>> query(@RequestBody SupplierContactQuery supplierContactQuery ) {
        PageBean bean = supplierService.query(supplierContactQuery);
        return success("供应商列表", bean);
    }


    /**
     * 根据id删除供应商基础信息
     * @param id
     * @return
     */
    @ApiOperation("删除供应商")
    @DeleteMapping("/supplier")
    public ResultBean deleteSupplierById(int id) {
        int delete = supplierService.delete(id);
        String tmp = delete > 0 ? "删除成功":"删除失败";
        return success("删除供应商",tmp);
    }

    /**
     * 重置供应商的密码
     * @param id
     * @return
     */
    @ApiOperation("重置供应商用户密码")
    @GetMapping("supplier/resetPwd")
    public ResultBean resetPassword(int id){
        int i = supplierService.resetPassword(id);
        String tmp = i > 0 ? "删除成功":"删除失败";
        return success("重置密码为123456",tmp);
    }

}
